package task02;

import java.util.Random;

public class Code0201 {

        // 定义行、列、二维数组
        private int rows;
        private int cols;
        private int[][] arr2;
        // 将行、列、对角线的和储存在一维数组中
        private int[] sumRows;
        private int[] sumCols;
        private int[] sumDiagonal;

        // 无参构造
        public Code0201() {
        }

        // 有参构造
        public Code0201(int rows, int cols) {
            setCols(cols);
            setRows(rows);
            setSumCols(cols);
            setSumRows(rows);
            setSumDiagonal(2);
            System.out.printf("=====正在创建%d行%d列的数组=====\n", rows, cols);
            arr2 = new int[rows][cols];
        }

        // 设置行
        public void setRows(int rows) {
            this.rows = rows;
        }

        // 设置列
        public void setCols(int cols) {
            this.cols = cols;
        }

        // 设置各行和
        public void setSumRows(int rows) {
            sumRows = new int[rows];
        }

        // 获取各列和
        public void setSumCols(int cols) {
            sumCols = new int[cols];
        }

        // 获取左上角至右下角和
        public void setSumDiagonal(int num) {
            sumDiagonal = new int[num];
        }

        // 获取行
        public int getRows() {
            return rows;
        }

        // 获取列
        public int getCols() {
            return cols;
        }

        // 获取各行和
        public int[] getSumRows() {
            System.out.println("\n====打印各行的和====");
            sumPrint(sumRows, "行");
            return sumRows;
        }

        // 获取各列和
        public int[] getSumCols() {
            System.out.println("\n====打印各列的和====");
            sumPrint(sumCols, "列");
            return sumCols;
        }

        // 获取对角线
        public int[] getSumDiagonal() {
            System.out.println("\n====打印对角线和====");
            System.out.println("注意：\n第01对角线为左上角至右下角");
            System.out.println("第02对角线为右上角至左下角\n");
            sumPrint(sumDiagonal, "对角线");
            return sumDiagonal;
        }

        // 打印数组
        public void showArr() {
            for (int[] arr1 : arr2) {
                for (int i = 0; i < arr1.length; i++) {
                    System.out.printf("%d ", arr1[i]);
                }
                System.out.println();
            }
        }

        // 添加元素，随机数0-3
        public void addValue() {
            Random rd = new Random();
            for (int i = 0; i < arr2.length; i++) {
                for (int j = 0; j < arr2[i].length; j++) {
                    arr2[i][j] = rd.nextInt(4);
                    sumRows[i] += arr2[i][j];
                    sumCols[j] += arr2[i][j];
                    if (i == j) {
                        sumDiagonal[0] += arr2[i][j];
                    }
                    if (i == (arr2[i].length - j - 1)) {
                        sumDiagonal[1] += arr2[i][j];
                    }
                }
            }
        }

        // 私有方法，打印行、列、对角线和
        private void sumPrint(int[] arr, String str) {
            for (int i = 0; i < arr.length; i++) {
                System.out.printf("第%02d%s的和是%d\n", i + 1, str, arr[i]);
            }
        }

    }
